const productService = require("../../service/admin/productService")

const productController = {
    add: async (req, res) => {
        console.log(req.file, req.body)
        const cover = req.file ? `/productuploads/${req.file.filename}` : ''
        const { userId, title, introduction, detail } = req.body
        // 调用service进行添加
        await productService.add({
            userId,
            title,
            introduction,
            detail,
            cover,
            editTime: new Date()
        })
        res.send({
            ActionType: 'ok'
        })
    },

    updateList: async (req, res) => {
        console.log(req.file, req.body)
        const cover = req.file ? `/productuploads/${req.file.filename}` : ''
        const { title, introduction, detail, _id } = req.body
        // 调用service进行添加
        await productService.updateList({
            _id,
            title,
            introduction,
            detail,
            cover,
            editTime: new Date()
        })
        res.send({
            ActionType: 'ok'
        })
    },

    getList: async (req, res) => {
        console.log(req.params.id)
        const result = await productService.getList({ _id: req.params.id })

        res.send({
            ActionType: 'ok',
            data: result
        })
    },

    delList: async (req, res) => {
        console.log(req.params.id)
        const result = await productService.delList({ _id: req.params.id })
        console.log(result)
        res.send({
            ActionType: 'ok',
        })
    },
}


module.exports = productController